package usbcali.edu.co.pqrs.presentation.backingBeans;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import usbcali.edu.co.pqrs.exceptions.ZMessManager;
import usbcali.edu.co.pqrs.modelo.MotReclSelect;
import usbcali.edu.co.pqrs.modelo.MotivoReclamacion;
import usbcali.edu.co.pqrs.modelo.SolicitudPqr;
import usbcali.edu.co.pqrs.modelo.dto.MotReclSelectDTO;
import usbcali.edu.co.pqrs.modelo.dto.MotivoReclamacionDTO;
import usbcali.edu.co.pqrs.modelo.dto.SolicitudPqrDTO;
import usbcali.edu.co.pqrs.presentation.businessDelegate.IBusinessDelegatorView;
import usbcali.edu.co.pqrs.utilities.FacesUtils;

/**
 * @author Zathura Code Generator http://code.google.com/p/zathura
 * 
 */
@ManagedBean
@ViewScoped
public class MotReclSelectView implements Serializable {
	private static final long serialVersionUID = 1L;

	private Long motivoReclamacion;
	private Long solicitudPqr;
	private SelectOneMenu txtSolicitudPqr;
	private SelectOneMenu txtMotivoReclamacion;
	private Map<String, String> losItemsSolicitud = new HashMap<String, String>();
	private Map<String, String> losItemsMotivoReclamacion = new HashMap<String, String>();
	private CommandButton btnBack;
	List<MotivoReclamacionDTO> data2;
	List<SolicitudPqrDTO> data3;

	private InputText txtIdMotRecl_MotivoReclamacion;
	private InputText txtIdSolPqr_SolicitudPqr;
	private InputText txtIdMotReclSelect;
	private CommandButton btnSave;
	private CommandButton btnModify;
	private CommandButton btnDelete;
	private CommandButton btnClear;
	private List<MotReclSelectDTO> data;
	private MotReclSelectDTO selectedMotReclSelect;
	private MotReclSelect entity;
	private boolean showDialog;
	@ManagedProperty(value = "#{BusinessDelegatorView}")
	private IBusinessDelegatorView businessDelegatorView;

	public MotReclSelectView() {
		super();
	}

	public void rowEventListener(RowEditEvent e) {
		try {
			MotReclSelectDTO motReclSelectDTO = (MotReclSelectDTO) e
					.getObject();

			if (txtIdMotRecl_MotivoReclamacion == null) {
				txtIdMotRecl_MotivoReclamacion = new InputText();
			}

			txtIdMotRecl_MotivoReclamacion.setValue(motReclSelectDTO
					.getIdMotRecl_MotivoReclamacion());

			if (txtIdSolPqr_SolicitudPqr == null) {
				txtIdSolPqr_SolicitudPqr = new InputText();
			}

			txtIdSolPqr_SolicitudPqr.setValue(motReclSelectDTO
					.getIdSolPqr_SolicitudPqr());

			if (txtIdMotReclSelect == null) {
				txtIdMotReclSelect = new InputText();
			}

			txtIdMotReclSelect.setValue(motReclSelectDTO.getIdMotReclSelect());

			Long idMotReclSelect = FacesUtils.checkLong(txtIdMotReclSelect);
			entity = businessDelegatorView.getMotReclSelect(idMotReclSelect);

			action_modify();
		} catch (Exception ex) {
		}
	}

	public String action_new() {
		action_clear();
		selectedMotReclSelect = null;
		setShowDialog(true);

		return "";
	}

	public String action_clear() {
		entity = null;
		selectedMotReclSelect = null;

		if (txtIdMotRecl_MotivoReclamacion != null) {
			txtIdMotRecl_MotivoReclamacion.setValue(null);
			txtIdMotRecl_MotivoReclamacion.setDisabled(true);
		}

		if (txtIdSolPqr_SolicitudPqr != null) {
			txtIdSolPqr_SolicitudPqr.setValue(null);
			txtIdSolPqr_SolicitudPqr.setDisabled(true);
		}

		if (txtIdMotReclSelect != null) {
			txtIdMotReclSelect.setValue(null);
			txtIdMotReclSelect.setDisabled(false);
		}

		if (btnSave != null) {
			btnSave.setDisabled(true);
		}

		if (btnDelete != null) {
			btnDelete.setDisabled(true);
		}

		return "";
	}

	public String action_back() {

		return "initialMenu.xhtml?faces-redirect=true";
	}

	public void listener_txtId() {
		try {
			Long idMotReclSelect = FacesUtils.checkLong(txtIdMotReclSelect);
			entity = (idMotReclSelect != null) ? businessDelegatorView
					.getMotReclSelect(idMotReclSelect) : null;
		} catch (Exception e) {
			entity = null;
		}

		if (entity == null) {
			txtIdMotRecl_MotivoReclamacion.setDisabled(false);
			txtIdSolPqr_SolicitudPqr.setDisabled(false);
			txtIdMotReclSelect.setDisabled(false);
			btnSave.setDisabled(false);
		} else {
			txtIdMotRecl_MotivoReclamacion.setValue(entity
					.getMotivoReclamacion().getIdMotRecl());
			txtIdMotRecl_MotivoReclamacion.setDisabled(false);
			txtIdSolPqr_SolicitudPqr.setValue(entity.getSolicitudPqr()
					.getIdSolPqr());
			txtIdSolPqr_SolicitudPqr.setDisabled(false);
			txtIdMotReclSelect.setValue(entity.getIdMotReclSelect());
			txtIdMotReclSelect.setDisabled(true);
			btnSave.setDisabled(false);

			if (btnDelete != null) {
				btnDelete.setDisabled(false);
			}
		}
	}

	public String action_edit(ActionEvent evt) {
		selectedMotReclSelect = (MotReclSelectDTO) (evt.getComponent()
				.getAttributes().get("selectedMotReclSelect"));
		txtIdMotRecl_MotivoReclamacion.setValue(selectedMotReclSelect
				.getIdMotRecl_MotivoReclamacion());
		txtIdMotRecl_MotivoReclamacion.setDisabled(false);
		txtIdSolPqr_SolicitudPqr.setValue(selectedMotReclSelect
				.getIdSolPqr_SolicitudPqr());
		txtIdSolPqr_SolicitudPqr.setDisabled(false);
		txtIdMotReclSelect.setValue(selectedMotReclSelect.getIdMotReclSelect());
		txtIdMotReclSelect.setDisabled(true);
		btnSave.setDisabled(false);
		setShowDialog(true);

		return "";
	}

	public String action_save() {
		try {
			if ((selectedMotReclSelect == null) && (entity == null)) {
				action_create();
			} else {
				action_modify();
			}

			data = null;
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_create() {
		try {
			entity = new MotReclSelect();

			MotivoReclamacion motivoReclamacionItem = businessDelegatorView
					.getMotivoReclamacion(motivoReclamacion);
			SolicitudPqr solicitudPqrItem = businessDelegatorView
					.getSolicitudPqr(solicitudPqr);

			entity.setMotivoReclamacion(motivoReclamacionItem);

			entity.setSolicitudPqr(solicitudPqrItem);

			businessDelegatorView.saveMotReclSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
			data = businessDelegatorView.getDataMotReclSelect();
			RequestContext.getCurrentInstance().update("form:listaTDo");
		} catch (Exception e) {
			entity = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modify() {
		try {
			if (entity == null) {
				Long idMotReclSelect = new Long(
						selectedMotReclSelect.getIdMotReclSelect());
				entity = businessDelegatorView
						.getMotReclSelect(idMotReclSelect);
			}

			entity.setMotivoReclamacion((FacesUtils
					.checkLong(txtIdMotRecl_MotivoReclamacion) != null) ? businessDelegatorView
					.getMotivoReclamacion(FacesUtils
							.checkLong(txtIdMotRecl_MotivoReclamacion)) : null);
			entity.setSolicitudPqr((FacesUtils
					.checkLong(txtIdSolPqr_SolicitudPqr) != null) ? businessDelegatorView
					.getSolicitudPqr(FacesUtils
							.checkLong(txtIdSolPqr_SolicitudPqr)) : null);
			businessDelegatorView.updateMotReclSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			data = null;
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_datatable(ActionEvent evt) {
		try {
			selectedMotReclSelect = (MotReclSelectDTO) (evt.getComponent()
					.getAttributes().get("selectedMotReclSelect"));

			Long idMotReclSelect = new Long(
					selectedMotReclSelect.getIdMotReclSelect());
			entity = businessDelegatorView.getMotReclSelect(idMotReclSelect);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_delete_master() {
		try {
			Long idMotReclSelect = FacesUtils.checkLong(txtIdMotReclSelect);
			entity = businessDelegatorView.getMotReclSelect(idMotReclSelect);
			action_delete();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public void action_delete() throws Exception {
		try {
			businessDelegatorView.deleteMotReclSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
			data = null;
		} catch (Exception e) {
			throw e;
		}
	}

	public String action_closeDialog() {
		setShowDialog(false);
		action_clear();

		return "";
	}

	public String actionDeleteDataTableEditable(ActionEvent evt) {
		try {
			selectedMotReclSelect = (MotReclSelectDTO) (evt.getComponent()
					.getAttributes().get("selectedMotReclSelect"));

			Long idMotReclSelect = new Long(
					selectedMotReclSelect.getIdMotReclSelect());
			entity = businessDelegatorView.getMotReclSelect(idMotReclSelect);
			businessDelegatorView.deleteMotReclSelect(entity);
			data.remove(selectedMotReclSelect);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
			action_clear();
		} catch (Exception e) {
			FacesUtils.addErrorMessage(e.getMessage());
		}

		return "";
	}

	public String action_modifyWitDTO(Long idMotReclSelect,
			Long idMotRecl_MotivoReclamacion, Long idSolPqr_SolicitudPqr)
			throws Exception {
		try {
			businessDelegatorView.updateMotReclSelect(entity);
			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
		} catch (Exception e) {
			// renderManager.getOnDemandRenderer("MotReclSelectView").requestRender();
			FacesUtils.addErrorMessage(e.getMessage());
			throw e;
		}

		return "";
	}

	public InputText getTxtIdMotRecl_MotivoReclamacion() {
		return txtIdMotRecl_MotivoReclamacion;
	}

	public void setTxtIdMotRecl_MotivoReclamacion(
			InputText txtIdMotRecl_MotivoReclamacion) {
		this.txtIdMotRecl_MotivoReclamacion = txtIdMotRecl_MotivoReclamacion;
	}

	public InputText getTxtIdSolPqr_SolicitudPqr() {
		return txtIdSolPqr_SolicitudPqr;
	}

	public void setTxtIdSolPqr_SolicitudPqr(InputText txtIdSolPqr_SolicitudPqr) {
		this.txtIdSolPqr_SolicitudPqr = txtIdSolPqr_SolicitudPqr;
	}

	public InputText getTxtIdMotReclSelect() {
		return txtIdMotReclSelect;
	}

	public void setTxtIdMotReclSelect(InputText txtIdMotReclSelect) {
		this.txtIdMotReclSelect = txtIdMotReclSelect;
	}

	public List<MotReclSelectDTO> getData() {
		try {
			if (data == null) {
				data = businessDelegatorView.getDataMotReclSelect();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return data;
	}

	public void setData(List<MotReclSelectDTO> motReclSelectDTO) {
		this.data = motReclSelectDTO;
	}

	public MotReclSelectDTO getSelectedMotReclSelect() {
		return selectedMotReclSelect;
	}

	public void setSelectedMotReclSelect(MotReclSelectDTO motReclSelect) {
		this.selectedMotReclSelect = motReclSelect;
	}

	public CommandButton getBtnSave() {
		return btnSave;
	}

	public void setBtnSave(CommandButton btnSave) {
		this.btnSave = btnSave;
	}

	public CommandButton getBtnModify() {
		return btnModify;
	}

	public void setBtnModify(CommandButton btnModify) {
		this.btnModify = btnModify;
	}

	public CommandButton getBtnDelete() {
		return btnDelete;
	}

	public void setBtnDelete(CommandButton btnDelete) {
		this.btnDelete = btnDelete;
	}

	public CommandButton getBtnClear() {
		return btnClear;
	}

	public void setBtnClear(CommandButton btnClear) {
		this.btnClear = btnClear;
	}

	public TimeZone getTimeZone() {
		return java.util.TimeZone.getDefault();
	}

	public IBusinessDelegatorView getBusinessDelegatorView() {
		return businessDelegatorView;
	}

	public void setBusinessDelegatorView(
			IBusinessDelegatorView businessDelegatorView) {
		this.businessDelegatorView = businessDelegatorView;
	}

	public boolean isShowDialog() {
		return showDialog;
	}

	public void setShowDialog(boolean showDialog) {
		this.showDialog = showDialog;
	}

	public void onEdit(org.primefaces.event.RowEditEvent event) {

		try {

			MotReclSelectDTO m = (MotReclSelectDTO) event.getObject();

			entity = null;
			entity = businessDelegatorView.getMotReclSelect(m
					.getIdMotReclSelect());

			motivoReclamacion = m.getIdMotRecl_MotivoReclamacion();
			solicitudPqr = m.getIdSolPqr_SolicitudPqr();

			MotivoReclamacion motivoReclamacionItem = businessDelegatorView
					.getMotivoReclamacion(motivoReclamacion);

			SolicitudPqr solicitudPqrItem = businessDelegatorView
					.getSolicitudPqr(solicitudPqr);

			entity.setSolicitudPqr(solicitudPqrItem);
			entity.setMotivoReclamacion(motivoReclamacionItem);

			businessDelegatorView.updateMotReclSelect(entity);

			data = businessDelegatorView.getDataMotReclSelect();

			RequestContext.getCurrentInstance().update("form:listaTDo");

			FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void onCancel(org.primefaces.event.RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Cancelado",
				((MotReclSelectDTO) event.getObject()).getIdMotReclSelect()
						+ "");

		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public Long getMotivoReclamacion() {
		return motivoReclamacion;
	}

	public void setMotivoReclamacion(Long motivoReclamacion) {
		this.motivoReclamacion = motivoReclamacion;
	}

	public Long getSolicitudPqr() {
		return solicitudPqr;
	}

	public void setSolicitudPqr(Long solicitudPqr) {
		this.solicitudPqr = solicitudPqr;
	}

	public SelectOneMenu getTxtSolicitudPqr() {
		return txtSolicitudPqr;
	}

	public void setTxtSolicitudPqr(SelectOneMenu txtSolicitudPqr) {
		this.txtSolicitudPqr = txtSolicitudPqr;
	}

	public SelectOneMenu getTxtMotivoReclamacion() {
		return txtMotivoReclamacion;
	}

	public void setTxtMotivoReclamacion(SelectOneMenu txtMotivoReclamacion) {
		this.txtMotivoReclamacion = txtMotivoReclamacion;
	}

	public Map<String, String> getLosItemsSolicitud() {
		try {
			if (data3 == null) {
				getData3();
				for (int i = 0; i < data3.size(); i++) {
					losItemsSolicitud.put(data3.get(i).getNumeroRadicacion(),
							data3.get(i).getIdSolPqr() + "");

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsSolicitud;
	}

	public void setLosItemsSolicitud(Map<String, String> losItemsSolicitud) {
		this.losItemsSolicitud = losItemsSolicitud;
	}

	public Map<String, String> getLosItemsMotivoReclamacion() {
		try {
			if (data2 == null) {
				getData2();
				for (int i = 0; i < data2.size(); i++) {
					losItemsMotivoReclamacion.put(data2.get(i)
							.getDescripcionMotRecl(), data2.get(i)
							.getIdMotRecl() + "");

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return losItemsMotivoReclamacion;
	}

	public void setLosItemsMotivoReclamacion(
			Map<String, String> losItemsMotivoReclamacion) {
		this.losItemsMotivoReclamacion = losItemsMotivoReclamacion;
	}

	public CommandButton getBtnBack() {
		return btnBack;
	}

	public void setBtnBack(CommandButton btnBack) {
		this.btnBack = btnBack;
	}

	public MotReclSelect getEntity() {
		return entity;
	}

	public void setEntity(MotReclSelect entity) {
		this.entity = entity;
	}

	public List<MotivoReclamacionDTO> getData2() {
		try {
			if (data2 == null) {
				data2 = businessDelegatorView.getDataMotivoReclamacion();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data2;
	}

	public void setData2(List<MotivoReclamacionDTO> data2) {
		this.data2 = data2;
	}

	public List<SolicitudPqrDTO> getData3() {
		try {
			if (data3 == null) {
				data3 = businessDelegatorView.getDataSolicitudPqr();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return data3;
	}

	public void setData3(List<SolicitudPqrDTO> data3) {
		this.data3 = data3;
	}

}
